import * as homeApi from '@/api/home.js';

export default {
  namespaced: true,
  state() {
    return {
      homeBanner: [],
      homeNewe: [],
      homeHot: [],
      homeCategoryHead: [],
      homeGoods: [],
      homeSpecial: [],
    };
  },
  mutations: {
    SET_HOME_BANNER(state, newValue) {
      state.homeBanner = newValue;
    },
    SET_HOME_NEWE(state, newValue) {
      state.homeNewe = newValue;
    },
    SET_HOME_HOT(state, newValue) {
      state.homeHot = newValue;
    },
    SET_HOME_CATEGORY_HEAD(state, newValue) {
      state.homeCategoryHead = newValue;
    },
    SET_HOME_GOODS(state, newValue) {
      state.homeGoods = newValue;
    },
    SET_HOME_SPECIAL(state, newValue) {
      state.homeSpecial = newValue;
    },
  },
  actions: {
    // 轮播图
    async updeteHomeBanner({ commit }) {
      const res = await homeApi.getHomeBanner();
      commit('SET_HOME_BANNER', res);
    },
    // 首页热门推荐
    async getHomeNewe({ commit }, limit) {
      const res = await homeApi.homeNewe(limit);
      commit('SET_HOME_NEWE', res);
    },
    // 首页人气推荐
    getHomeHot({ commit }) {
      homeApi.homeHot()
        .then((res) => {
          commit('SET_HOME_HOT', res);
        })
        .catch(() => ({}));
    },
    // 首页全部分类
    updateHomeCategoryHead({ commit }) {
      homeApi.homeCategoryHead()
        .then((res) => {
          commit('SET_HOME_CATEGORY_HEAD', res);
        })
        .catch(() => ({}));
    },
    // 首页部分分类
    updateHomeGoods({ commit }) {
      homeApi.homeGoods()
        .then((res) => {
          commit('SET_HOME_GOODS', res);
        })
        .catch(() => ({}));
    },
    // 首页专题精选
    updateHomeSpecial({ commit }) {
      homeApi.homeSpecial()
        .then((res) => {
          commit('SET_HOME_SPECIAL', res);
        })
        .catch(() => ({}));
    },
  },
};
